import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

// Product类
class Product {
    private String productName;
    private double price;
    private int quantity;

    public Product(String productName, double price, int quantity) {
        this.productName = productName;
        this.price = price;
        this.quantity = quantity;
    }

    public double getTotalPrice() {
        return price * quantity;
    }

    // Getters
    public String getProductName() { return productName; }
    public double getPrice() { return price; }
    public int getQuantity() { return quantity; }
}

// Order类
class Order {
    private String orderId;
    private String customerName;
    private LocalDateTime orderDate;
    private List<Product> products;

    public Order(String orderId, String customerName, LocalDateTime orderDate, List<Product> products) {
        this.orderId = orderId;
        this.customerName = customerName;
        this.orderDate = orderDate;
        this.products = products;
    }

    public double getTotalAmount() {
        return products.stream()
                .mapToDouble(Product::getTotalPrice)
                .sum();
    }

    // Getters
    public String getOrderId() { return orderId; }
    public String getCustomerName() { return customerName; }
    public LocalDateTime getOrderDate() { return orderDate; }
    public List<Product> getProducts() { return products; }
}

// 订单处理系统
public class OrderProcessingSystem {
    // 筛选订单并计算总金额
    public static Map<String, Double> filterAndCalculateTotalAmounts(
            List<Order> orders,
            LocalDateTime startDate,
            LocalDateTime endDate,
            String customerNameKeyword) {
        return orders.stream()
                .filter(order -> !order.getOrderDate().isBefore(startDate) &&
                        !order.getOrderDate().isAfter(endDate) &&
                        order.getCustomerName().contains(customerNameKeyword))
                .collect(Collectors.toMap(
                        Order::getOrderId,
                        Order::getTotalAmount,
                        (v1, v2) -> v1, // 处理重复key（理论上orderId应唯一）
                        LinkedHashMap::new
                ));
    }

    // 找出总金额最高的订单
    public static Optional<Order> findHighestAmountOrder(List<Order> orders) {
        return orders.stream()
                .max(Comparator.comparingDouble(Order::getTotalAmount));
    }

    // 按日期升序、金额降序排序
    public static List<Order> sortOrdersByDateAndAmount(List<Order> orders) {
        return orders.stream()
                .sorted(Comparator.comparing(Order::getOrderDate)
                        .thenComparing(Order::getTotalAmount, Comparator.reverseOrder()))
                .collect(Collectors.toList());
    }

    // 测试主方法
    public static void main(String[] args) {
        // 创建测试数据
        List<Order> orders = Arrays.asList(
                new Order("O001", "张伟",
                        LocalDateTime.of(2025, 1, 1, 10, 0),
                        Arrays.asList(
                                new Product("手机", 1000.0, 2),
                                new Product("耳机", 200.0, 1)
                        )),
                new Order("O002", "张丽",
                        LocalDateTime.of(2025, 1, 2, 12, 0),
                        Arrays.asList(
                                new Product("电脑", 5000.0, 1)
                        )),
                new Order("O003", "李明",
                        LocalDateTime.of(2025, 1, 1, 15, 0),
                        Arrays.asList(
                                new Product("平板", 2000.0, 1),
                                new Product("键盘", 150.0, 2)
                        )),
                new Order("O004", "张强",
                        LocalDateTime.of(2025, 2, 1, 9, 0),
                        Arrays.asList(
                                new Product("显示器", 1500.0, 1)
                        ))
        );

        // 1. 筛选特定日期范围和客户姓名的订单并计算总金额
        LocalDateTime startDate = LocalDateTime.of(2025, 1, 1, 0, 0);
        LocalDateTime endDate = LocalDateTime.of(2025, 1, 29, 23, 59);
        String keyword = "张";

        Map<String, Double> orderTotals = filterAndCalculateTotalAmounts(orders, startDate, endDate, keyword);
        System.out.println("筛选出的订单总金额:");
        orderTotals.forEach((id, total) ->
                System.out.println("订单编号: " + id + ", 总金额: " + total));

        // 2. 找出总金额最高的订单 (Java 8 compatible)
        Optional<Order> highestOrder = findHighestAmountOrder(orders);
        if (highestOrder.isPresent()) {
            Order order = highestOrder.get();
            System.out.println("\n最高金额订单: " + order.getOrderId() +
                    ", 总金额: " + order.getTotalAmount());
        } else {
            System.out.println("\n没有找到订单");
        }

        // 3. 按日期和金额排序
        System.out.println("\n排序后的订单:");
        List<Order> sortedOrders = sortOrdersByDateAndAmount(orders);
        sortedOrders.forEach(order ->
                System.out.println("订单编号: " + order.getOrderId() +
                        ", 日期: " + order.getOrderDate() +
                        ", 总金额: " + order.getTotalAmount()));
    }
}